package lec07datastructure.v2;

public class MyVector {
    private static final int DEFAULT_SIZE = 100;
    int[] data = new int[DEFAULT_SIZE];
    int size = 0;

    public static void main(String[] args) {
        MyVector v = new MyVector();
        for (int i = 0; i < 1000; i++) {
            v.pushBack(i);
        }
        System.out.println(v.get(0));
        System.out.println(v.get(v.size - 1));
    }

    public int get(int x) {
        if (x < size)
            return data[x];
        return -1;
    }

    void set(int x, int val) {
        if (x < size)
            data[x] = val;
    }

    public void show() {
        for (int i = 0; i < size; i++) {
            System.out.print(data[i] + ",");
        }
        System.out.println();
    }

    public void pushBack(int i) {
        if (size == data.length) {
            int[] data1 = new int[data.length * 2];
            System.arraycopy(data, 0, data1, 0, data.length);
            data = data1;
        }
        data[size++] = i;
    }

    public void popBack() {
        if (size == 0)
            return;
        --size;
    }

    public int back() {
        if (size > 0)
            return data[size - 1];
        return -1;
    }
}
